Mif.Tree.implement({

	getTarget: function(event){
		var target = event.target;
		var cls = target.className;
		while(!(/mif-tree/).test(target.className)){
			target = target.parentNode;
		}
		var test = target.className.match(/mif-tree-(gadjet)-[^n]|mif-tree-(name)/);
		if(!test){
			return {
				node: false,
				target: 'node'
			};
		}
		for(var i = 3; i > 0; i--){
			if(test[i]){
				var type = test[i];
				break;
			}
		}
		if(type == 'name' && cls != 'tag'){
			return {
				node: false,
				target: 'node'
			};
		}
		return {
			node: Mif.Tree.Nodes[target.getAttribute('uid')],
			target: type
		};
	}

});

Mif.Tree.Node.prototype.getNextVisible_ = Mif.Tree.Node.prototype.getNextVisible;
Mif.Tree.Node.prototype.getPreviousVisible_ = Mif.Tree.Node.prototype.getPreviousVisible;

Mif.Tree.Node.implement({

	hide: function(){
		this.state.hidden = true;
		this.getDOM('node').style.display = 'none';
	},
	
	show: function(){
		this.state.hidden = false;
		this.getDOM('node').style.display = 'block';
	},
	
	isHide: function(){
		return this.state.hidden;
	},
	
	getNextVisible: function(){
		var node = this;
		do{
			node = node.getNextVisible_();
			if(!node) return false;
		}while(node.isHide())
		return node;
	},
	
	getPreviousVisible: function(){
		var node = this;
		do{
			node = node.getPreviousVisible_();
			if(!node) return false;
		}while(node.isHide())
		return node;
	}

});


Mif.Tree.KeyNav.implement({

	goForward: function(current){
		var forward = current;
		do{
			forward = forward.getNextVisible();
			if(!forward) return;
		}while(forward.type == 'close');
		if( forward ) this.tree.select(forward);
	},
	
	goBack: function(current){
		var back = current;
		do{
			back = back.getPreviousVisible();
			if(!back) return;
		}while(back.type == 'close');
		if (back) this.tree.select(back);
	}

});


var DOMTree = null;
window.onload=function(){
	var tree_cont = $('tree_container');
	DOMTree = new Mif.Tree({
		container: tree_cont,
		forest: true,
		types: {
			open:{
				cls: 'tag-open',
				loadable: true
			},
			close:{
				loadable: false,
				cls: 'tag-close'
			}
		},
		dfltType: 'open',
		height: 18,
		initialize: function(){
			new Mif.Tree.KeyNav(this);
		},
		onToggle: function(node, state){
			var next = node.getNext();
			if(!next) return;
			next[state ? 'show' : 'hide']();
		},
		onDrawChildren: function(parent){
			parent.children.each(function(child){
				if(child.type != 'close') return;
				child.hide();
			});
		},
		onSelect: function(node){
			showDetail(node.name);
			if(node.type == 'close'){
				node.tree.select(node.getPrevious());
			}
			this.wrapper.focus();
		}
	});
	
	loadDOMTree();
}



function showDetail(a){
	if (a.indexOf("hierarchy") > 0) {
		return;
	}
	var text = a.substring(a.indexOf("text"),a.indexOf("resource-id"));
	var resource_id = a.substring(a.indexOf("resource-id"),a.indexOf("class=\"<"));
	var class_name =  a.substring(a.indexOf("class=\"<"),a.indexOf("package"));
	var package = a.substring(a.indexOf("package"),a.indexOf("content-desc"));
	var bounds = a.substring(a.indexOf("bounds"),a.lastIndexOf("]")+7);
	
	text = text.substring(text.indexOf("attr-value")+12,text.lastIndexOf("span")-2);
	resource_id = resource_id.substring(resource_id.indexOf("attr-value")+12,resource_id.lastIndexOf("span")-2);
	class_name = class_name.substring(class_name.indexOf("attr-value")+12,class_name.lastIndexOf("span")-2);
	package = package.substring(package.indexOf("attr-value")+12,package.lastIndexOf("span")-2);
	bounds = bounds.substring(bounds.indexOf("attr-value")+12,bounds.lastIndexOf("span")-2);
	
	//alert(text + "|" + resource_id +"|" + class_name + "|" + package + "|" + bounds);
	var s_html = "<div>text:<a>"+text+"</a></div>";
	s_html += "<div>resource_id:<a>"+resource_id+"</a></div>";
	s_html += "<div>class:<a>"+class_name+"</a></div>";
	s_html += "<div>package:<a>"+package+"</a></div>";
	s_html += "<div>bounds:<a>"+bounds+"</a></div>";
	
	var tree_description = $('tree_description');
	tree_description.empty();
	tree_description.innerHTML = s_html;
	
	showPictureLine(bounds);
}

function showPictureLine(bounds){
	var w1 = bounds.substring(bounds.indexOf("[")+1,bounds.indexOf(","));
	var h1 = bounds.substring(bounds.indexOf(",")+1,bounds.indexOf("]"));
	var w2 = bounds.substring(bounds.lastIndexOf("[")+1,bounds.lastIndexOf(","));
	var h2 = bounds.substring(bounds.lastIndexOf(",")+1,bounds.lastIndexOf("]"));
	w1 = Number(w1)/2;
	h1 = Number(h1)/2;
	w2 = Number(w2)/2;
	h2 = Number(h2)/2;
	
	var s_html = "<div style='top:"+ h1 +"px;left:"+ w1 +"px;height:"+ (h2-h1) +"px;width:"+ (w2-w1) +"px;'></div>";
	var imgdiv = $('imgdiv');
	imgdiv.empty();
	imgdiv.innerHTML = s_html;
}

/*jiayazhou.2017.8.25*/
function loadXMLFile(){
	var url= window.location.href;
	var baseUrl = url.substring(0,url.lastIndexOf("/")+1);
	var file = baseUrl+"uiautomator.xml";
	//xml.loadXMLFile(baseUrl+"v600.xml");
	var xmlDoc=null;
	//判断浏览器的类型
	//支持IE浏览器
	if(!window.DOMParser && window.ActiveXObject){
		var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
		for(var i=0;i<xmlDomVersions.length;i++){
			try{
				xmlDoc = new ActiveXObject(xmlDomVersions[i]);
				xmlDoc.async=false;  
				xmlDoc.load(file);  
				break;
			}catch(e){
			}
		}
	}
	else{
		try //Firefox, Mozilla, Opera, etc.  
		{  
			xmlDoc=document.implementation.createDocument("","",null);  
			xmlDoc.async=false;  
			xmlDoc.load(file);  
		}  
		catch(e)  
		{
			try //Google Chrome  
			{  
				var xmlhttp = new window.XMLHttpRequest();  
				xmlhttp.open("GET",file,false);  
				xmlhttp.send(null);  
				xmlDoc = xmlhttp.responseXML.documentElement;  
			}  
			catch(e)  
			{  
				error=e.message;  
			}  
		}  
	}
	return xmlDoc;
}

//modify by jiayazhou
function loadDOMTree(){
DOMTree.load({
	json: [{
		property: {
			name: '&lt;<span class="tag">hierarchy</span>&gt;'
		},
		data:{
			el: loadXMLFile()//$(document.documentElement)
		}
	},
	{
		property: {
			name: '&lt;/<span class="tag">hierarchy</span>&gt;'
		},
		type: 'close'
	}]
});

DOMTree.loadOptions = function(node){
	var json = [];
	var el = node.data.el;
	el.getChildren().each(function(child){
		var attrs = '';
		$A(child.attributes).each(function(attr){
			attrs += " " + attr.nodeName + '="<span class="attr-value">' + attr.nodeValue + '</span>"';
		});
		var hasChildren = child.getChildren().length > 0;
		var name;
		if(!hasChildren){
			var text = child.firstChild ? child.firstChild.nodeValue : '';
			name = '&lt;<span class="tag">' + child.get('tag') + '</span>' + attrs + '&gt;<span class="value">' + text +  '</span>&lt;/<span class="tag">' + child.get('tag') + '</span>&gt;';
			var loadable = false;
		}else{
			name = '&lt;<span class="tag">' + child.get('tag') + '</span>' + attrs + '&gt;';
			loadable = true;
		}
		json.push({
			property: {
				name: name,
				loadable: loadable
			},
			data: {
				el: child
			}
		});
		json.push({
			property: {
				name: '&lt;/<span class="tag">' + child.get('tag') + '</span>&gt;'
			},
			type: 'close'
		});
	});
	return {json: json};
};
}

if(Browser.Engine.trident){
	(new Element('div', {styles:{color: 'red', fontSize: '20px', paddingLeft: '20px'}}).set('html', 'LOL IE')).injectBefore($('tree_container'));
};
